package other.xsy.passlys.receivable;

import com.rkhd.platform.sdk.data.model.InvoiceDetail__c;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.trigger.DataResult;
import com.rkhd.platform.sdk.trigger.Trigger;
import com.rkhd.platform.sdk.trigger.TriggerRequest;
import com.rkhd.platform.sdk.trigger.TriggerResponse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description 删除开票明细，重新计算开票相关金额字段
 * @Author 黎炎松
 * 2024-08-08 18:38
 */
public class ClaimDetailDel implements Trigger {
    private static final Logger logger = LoggerFactory.getLogger();

    @Override
    public TriggerResponse execute(TriggerRequest request) throws ScriptBusinessException {
        logger.debug("删除开票明细 触发开始 ");
        List<XObject> dataList = request.getDataList();
        List<DataResult> dataResultList = new ArrayList<>();
        List<InvoiceDetail__c> invoiceDetailList = new ArrayList<>();
        for (XObject xObject : dataList) {
            InvoiceDetail__c invoiceDetail = (InvoiceDetail__c) xObject;
            invoiceDetailList.add(invoiceDetail);

            dataResultList.add(new DataResult(true, "", xObject));
        }

        // 触发修改相关认领字段
        Long booking = invoiceDetailList.get(0).getBooking__c();
        if (booking != null) {
            ReceivableService receivableService = new ReceivableService();
            receivableService.calcClaimAmounByBooking(Arrays.asList(booking));
        }

        return new TriggerResponse(true, "处理完成", dataResultList);
    }

}
